/**
 * @file generateDataForTree
 * @author  fanxl
 */
import {cloneDeep, pick, assign} from 'lodash-es';

/**
 * ts类型后面会再补充
 */
export const generateDataForTree = (list: Array<Record<string, any>>, prefix: string = '') => {
    const copyList = cloneDeep(list);
    const recursionChange = (list: Array<Record<string, any>>, father = '') => {
        for (let i = 0; i < list.length; i++) {
            let item = list[i];
            // 对于alternative类型，把enumValues里的数据做处理，枚举出来
            if (['ALTERNATIVE', 'ENUM'].includes(item.dataType)) {
                item = assign(item, {
                    items: item.enumValues.map((el: any) => assign(el, {name: el.value}))
                });
            }
            let currentPath = father ? `${father}.${item.name}` : item.name;
            // list类型，如果没有items只有element的，特殊处理子节点的name为'0'
            if (item.dataType === 'LIST') {
                // list 节点本身的path不需要带[*],list的子节点需要带[*]
                item = assign(item, {
                    value: currentPath,
                    label: item.displayName || item.name
                });
                currentPath += '[*]';
            } else if (item.name) {
                item = assign(item, {
                    value: currentPath,
                    label: item.displayName || item.name
                });
            }
            if (item.items && item.items.length) {
                item.options = item.items;
                recursionChange(item.items, currentPath);
            }
            item.spec = cloneDeep(item);
            list[i] = pick(list[i], ['value', 'label', 'options', 'spec']);
        }
        return list;
    };
    return recursionChange(copyList, prefix);
};
